﻿using System;
using System.Collections.Generic;
using System.Text;
using AspNetCs.SQLServerDAL;
using AspNetCs.Model;
using System.Data;
using AspNetCs.IDAL;
using AspNetCs.DALFactory;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AspNetCs.Common;
using AspNetCs.Enumerations.User_Enum;

namespace AspNetCs.Bll
{
    public class MusicBLL
    {
        private IMusic musicDAL = Conn.CreateMusic();

        public DataTable GetDataTable(string filter)
        {
            return musicDAL.GetDataTable(filter);
        }


        public MusicModel GetModel(DataRow row)
        {
            return musicDAL.GetModel(row);
        }
        public MusicModel GetModel(int id)
        {
            MusicModel md = new MusicModel();
            DataTable dt = GetDataTable(" id=" + id);
            foreach (DataRow dr in dt.Rows)
            {
                md = musicDAL.GetModel(dr);
            }
            return md;

        }


        /// <summary>
        /// 查询所有的音乐
        /// </summary>
        /// <returns></returns>
        public DataTable queryAllMusic()
        {
            return musicDAL.queryAllMusic();
        }

        /// <summary>
        /// 添加音乐
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int AddMusic(MusicModel model)
        {
            return musicDAL.AddMusic(model);
        }

        /// <summary>
        /// 修改音乐
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UpdateMusic(MusicModel model)
        {
            return musicDAL.UpdateMusic(model);
        }

        /// <summary>
        /// 删除音乐
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public int DeleteMusic(int Id)
        {
            return musicDAL.DeleteMusic(Id);
        }

        /// <summary>
        /// 绑定背景音乐
        /// </summary>
        /// <param name="ddlMusic"></param>
        public DropDownList BindMusic(DropDownList ddlMusic)
        {
            foreach (ListItem item in ddlMusic.Items)
            {
                ddlMusic.Items.Remove(item);
            }
            ListItem firstItem = new ListItem();
            firstItem.Value = "0";
            firstItem.Text = "--请选择背景音乐--";
            ddlMusic.Items.Add(firstItem);

            DataTable dataTable = queryAllMusic();
            foreach (DataRow row in dataTable.Rows)
            {
                ListItem listItem = new ListItem();
                listItem.Value = row["id"].ToString();
                listItem.Text = row["MusicName"].ToString();
                ddlMusic.Items.Add(listItem);
            }
            return ddlMusic;

        }

        public void BindSelectedMusic(int MusicId, DropDownList ddlMusic)
        {
            ddlMusic = BindMusic(ddlMusic);
            ddlMusic.SelectedValue = "0";
            foreach (ListItem item in ddlMusic.Items)
            {
                if (StringHelper.isNumber_int(item.Value) == MusicId)
                {
                    ddlMusic.SelectedValue = item.Value;
                }
            }
        }

        /// <summary>
        /// 查找音乐的路径
        /// </summary>
        /// <param name="MusicId"></param>
        /// <returns></returns>
        public string MusicPath(int MusicId)
        {
            string MusicStr = "";
            try
            {
                if (MusicId > 0)
                {

                    MusicModel MusicMd = Music_Bll.CreateNew.GetModel(MusicId);
                    if (MusicMd.MusicType == (int)MusicTypeEnum.me)
                    {
                        if (MusicMd.MusicPath + "" != "")
                        {
                            MusicStr = System_Lable.SystemGetCache.InstallDir + MusicMd.MusicPath;
                        }
                    }
                    else
                    {
                        MusicStr = MusicMd.MusicPath;

                    }
                }
            }
            catch
            {
                MusicStr = "";

            }
            return MusicStr;
        }
    }
}
